package com.empire.module.member.service.personwarningsummary;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.empire.module.member.controller.admin.personwarningsummary.vo.*;
import com.empire.module.member.dal.dataobject.personwarningsummary.PersonWarningSummaryDO;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.util.object.BeanUtils;

import com.empire.module.member.dal.mysql.personwarningsummary.PersonWarningSummaryMapper;

import javax.annotation.Resource;

import static com.empire.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.empire.module.member.enums.ErrorCodeConstants.*;

/**
 * 人员预警统计汇总 Service 实现类
 *
 * @author 主席大人
 */
@Service
@Validated
public class PersonWarningSummaryServiceImpl implements PersonWarningSummaryService {

    @Resource
    private PersonWarningSummaryMapper personWarningSummaryMapper;

    @Override
    public Long createPersonWarningSummary(PersonWarningSummarySaveReqVO createReqVO) {
        // 插入
        PersonWarningSummaryDO personWarningSummary = BeanUtils.toBean(createReqVO, PersonWarningSummaryDO.class);
        personWarningSummaryMapper.insert(personWarningSummary);
        // 返回
        return personWarningSummary.getId();
    }

    @Override
    public void updatePersonWarningSummary(PersonWarningSummarySaveReqVO updateReqVO) {
        // 校验存在
        validatePersonWarningSummaryExists(updateReqVO.getId());
        // 更新
        PersonWarningSummaryDO updateObj = BeanUtils.toBean(updateReqVO, PersonWarningSummaryDO.class);
        personWarningSummaryMapper.updateById(updateObj);
    }

    @Override
    public void deletePersonWarningSummary(Long id) {
        // 校验存在
        validatePersonWarningSummaryExists(id);
        // 删除
        personWarningSummaryMapper.deleteById(id);
    }

    private void validatePersonWarningSummaryExists(Long id) {
        if (personWarningSummaryMapper.selectById(id) == null) {
            throw exception(PERSON_WARNING_SUMMARY_NOT_EXISTS);
        }
    }

    @Override
    public PersonWarningSummaryDO getPersonWarningSummary(Long id) {
        return personWarningSummaryMapper.selectById(id);
    }

    @Override
    public PageResult<PersonWarningSummaryDO> getPersonWarningSummaryPage(PersonWarningSummaryPageReqVO pageReqVO) {
        return personWarningSummaryMapper.selectPage(pageReqVO);
    }

}